<?php

require_once 'phpmoko/ui/RootPanel.php';
require_once 'phpmoko/ui/Widget.php';

class Image extends Widget
{
	private $imageSmall = null;
	private $imageBig = null;
	private $groupId = null;
	private $title = null;

	private static $groups = array();

	function __construct($imageSmall)
	{
		parent::__construct();

		$this->id = RootPanel::getUniqueId();

		$this->imageSmall = $imageSmall;

		$this->setStyleName('pmk-Image');
	}

	function setUrlBigImage($imageBig)
	{
		require_once 'phpmoko/externals/JQueryFancyBox.php';
		JQueryFancyBox::activate();

		$this->imageBig = $imageBig;
	}

	function setGroupId($groupId)
	{
		$this->groupId = $groupId;
		$this->id = $groupId;
	}

	function setTitle($title)
	{
		$this->title = $title;
	}

	function toString()
	{
		$str = '';

		if ($this->imageBig != null)
		{
			$str .= '<a';

			if ($this->groupId != null)
				$str .= ' class="'.$this->groupId.'" rel="'.$this->groupId.'"';
			else
				$str .= ' id="'.$this->id.'"';

			if ($this->title != null)
				$str .= ' title="'.$this->title.'"';

			$str .= ' href="'.$this->imageBig.'">';
		}

		$str .= '<img src="'.$this->imageSmall.'"';

		if ($this->groupId != null)
			$str .= ' alt="'.$this->groupId.'"';

		if ($this->getWidth() != null)
			$str .= ' width="'.$this->getWidth().'"';

		if ($this->getHeight() != null)
			$str .= ' height="'.$this->getHeight().'"';

		if ($this->style != null)
			$str .= ' class="'.$this->style.'"';

		$str .= '>';
			
		if ($this->imageBig != null)
			$str .= '</a>';
			
		if ($this->imageBig != null)
		{
			require_once 'phpmoko/ui/RootPanel.php';
			$root = RootPanel::get();

			if ($this->groupId != null)
			{
				if (!in_array($this->groupId, self::$groups))
				{
					$root->addJSOnReadyDocument('$(".'.$this->groupId.'").fancybox({prevEffect: \'none\', nextEffect: \'none\', helpers	: {	title	: { type: \'outside\' }, overlay	: { opacity : 0.8, css : { \'background-color\' : \'#000\' } }, thumbs	: { width	: 50, height	: 50}}});');
					self::$groups[] = $this->groupId;
				}
			}
			else
			{
				$root->addJSOnReadyDocument('$("a#'.$this->id.'").fancybox({\'transitionIn\': \'none\', \'transitionOut\': \'none\', \'titlePosition\': \'over\'});');
			}
		}
			
		return $str;
	}
}
?>